using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;

namespace BetterEventViewer.Gui
{
    public class MinimizedApplicationContext : ApplicationContext
    {
        private IContainer components;
        private NotifyIcon notifyIcon;
        private Form mainForm;

        public MinimizedApplicationContext()
        {
            Init();
        }

        private void Init()
        {
            components = new Container();
            notifyIcon = new NotifyIcon(components);

            notifyIcon.Text = "BetterEventViewer";
            notifyIcon.BalloonTipText = "BetterEventViewer";
            notifyIcon.ShowBalloonTip(1000000,"BetterEventViewer","BetterEventViewer is running in the tray...",ToolTipIcon.Info);
            notifyIcon.Icon = new System.Drawing.Icon(typeof(MinimizedApplicationContext), "MinimizeIcon.ico");
            notifyIcon.Visible = true;

            notifyIcon.DoubleClick += new EventHandler(notifyIcon_DoubleClick);
        }

        void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            ShowMainForm();
        }
          
        private void ShowMainForm()
        {
            if (mainForm == null)
            {
                mainForm = new MainForm();
                mainForm.Show();

                mainForm.Closed += new EventHandler(MainForm_Closed);
            }
            else
            {
                mainForm.Activate();
            }
        }

        private void MainForm_Closed(object s, System.EventArgs e)
        {
            mainForm = null;
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
        }
    }
}
